package com.lcxw.hxzbadmin.scheduleTask;

import com.alibaba.fastjson.JSON;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.DependsOn;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.lcxw.hxzbapi.entity.pojo.GiftSend;
import com.lcxw.hxzbadmin.entity.ranklist.RoomRankBean;
import com.lcxw.hxzbapi.service.GiftSendService;
import com.lcxw.hxzbapi.entity.pojo.YwpdMember;
import com.lcxw.hxzbapi.entity.pojo.YwpdRoom;
import com.lcxw.hxzbapi.service.YwpdMemberService;
import com.lcxw.hxzbapi.service.YwpdRoomService;
import com.lcxw.hxzbcommon.common.RedisUtils;

@Component
public class GiftEarnCountTask {

    private Logger logger = LoggerFactory.getLogger(GiftEarnCountTask.class);

    @Resource
    GiftSendService giftSendService;

    @Resource
    YwpdRoomService ywpdRoomService;

    @Resource
    RedisUtils redisUtils;

    @Resource
    YwpdMemberService ywpdMemberService;

    /**
     * 每小时房间收获的榜单
     */
    @Scheduled(cron = "0 0 * * * *")
    public void hourCount() {
        String redisKey = "roomEarn:Range:hour";

        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.HOUR, -1);
        Date d = c.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = sdf.format(d);

        roomRangeCount(redisKey, time);

    }

    /**
     * 每天房间收获的榜单
     */
    @Scheduled(cron = "0 0 0 * * *")
    public void dayCount() {

        String redisKey = "roomEarn:Range:day";

        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.DATE, -1);
        Date d = c.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = sdf.format(d);

        roomRangeCount(redisKey, time);

    }

    /**
     * 每周房间收获的榜单
     */
    @Scheduled(cron = "0 0 0 ? * 1")//每周一0点0分0秒
    public void weekCount() {

        String redisKey = "roomEarn:Range:week";

        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.DATE, -7);
        Date d = c.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = sdf.format(d);

        roomRangeCount(redisKey, time);

    }

    /**
     * 每月房间收获的榜单
     */
//    @Scheduled(cron = "2/8 * * * * *")
    @Scheduled(cron = "0 0 0 1 * ?")
    public void monthCount() {

        String redisKey = "roomEarn:Range:month";

        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.MONTH, -1);
        Date d = c.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = sdf.format(d);

        roomRangeCount(redisKey, time);

    }


    private void roomRangeCount(String redisKey, String time) {
        redisUtils.list_delete(redisKey);
        try {
            List<GiftSend> giftSendList = giftSendService.selectAfterTime(time);

            Map<Long, Integer> roomGained = new TreeMap<>();

            for (GiftSend giftSend : giftSendList) {
                YwpdRoom ywpdRoom = ywpdRoomService.selectRoomByRoomId(giftSend.getRoomId());
                if (null == ywpdRoom){
                    continue;
                }

                long roomId = ywpdRoom.getRoomId();
                int giftNum = giftSend.getGiftNum();
                int needCoin = giftSend.getNeedCoin();

                if (roomGained.containsKey(roomId)) {
                    int count = roomGained.get(roomId);
                    count += giftNum * needCoin;
                    roomGained.put(roomId, count);
                } else {
                    roomGained.put(roomId, giftNum * needCoin);
                }

            }

            List<Entry<Long, Integer>> list = new ArrayList<>(roomGained.entrySet());

            ValueComparator valueComparator = new ValueComparator();

            list.sort(valueComparator);

            Entry<Long, Integer> tmp = null;

            for (Entry<Long, Integer> longIntegerEntry : list) {
                tmp = longIntegerEntry;
                long roomId = tmp.getKey();
                RoomRankBean roomRankBean = new RoomRankBean();
                roomRankBean.setRoomId(roomId);
                roomRankBean.setGain(roomGained.get(roomId) * 10); //这里也将赚取到的转换成火力值，即收到礼物的总价值*10
                YwpdRoom ywpdRoom = ywpdRoomService.selectRoomByRoomId(roomId);
                roomRankBean.setBakPicUrl(ywpdRoom.getBakPicUrl());
                roomRankBean.setRoomName(ywpdRoom.getRoomName());
                roomRankBean.setRoomType(ywpdRoom.getRoomType());

                long userId = ywpdRoom.getUserId();
                roomRankBean.setUserId(userId);

                YwpdMember ywpdMember = ywpdMemberService.selectYwpdMemberByUserId(userId);
                roomRankBean.setImgUrl(ywpdMember.getImgUrl());
                roomRankBean.setNickName(ywpdMember.getNickName());

                String redisValue = JSON.toJSONString(roomRankBean);
                redisUtils.list_rightPush(redisKey, redisValue);

            }


        } catch (Exception e) {
            e.printStackTrace();
            logger.error("get giftEarn failed");
        }
    }


    private static class ValueComparator implements Comparator<Entry<Long, Integer>> {

        @Override
        public int compare(Map.Entry<Long, Integer> m, Map.Entry<Long, Integer> n) {
            return n.getValue() - m.getValue();
        }
    }
}
